//给定一棵二叉树的根节点 root ，请找出该二叉树中每一层的最大值。 
//
// 
//
// 示例1： 
//
// 
//
// 
//输入: root = [1,3,2,5,3,null,9]
//输出: [1,3,9]
// 
//
// 示例2： 
//
// 
//输入: root = [1,2,3]
//输出: [1,3]
// 
//
// 
//
// 提示： 
//
// 
// 二叉树的节点个数的范围是 [0,10⁴] 
// -2³¹ <= Node.val <= 2³¹ - 1 
// 
//
// 
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 217 👎 0

package leetcode.editor.cn;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class _515_FindLargestValueInEachTreeRow {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static void main(String[] args) {
        Solution solution = new _515_FindLargestValueInEachTreeRow().new Solution();
        TreeNode root = new TreeNode(1,
                new TreeNode(3, new TreeNode(5), new TreeNode(3)),
                new TreeNode(2, null, new TreeNode(9)));
        System.out.println(solution.largestValues(root));
    }

    /**
     * 深度优先遍历
     */
    class Solution {
        public List<Integer> largestValues(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            dfs(root, res, 0);
            return res;
        }

        private void dfs(TreeNode root, List<Integer> res, int curHeight) {
            if (root == null) return;
            if (curHeight >= res.size()) {
                res.add(root.val);
            } else {
                res.set(curHeight, Math.max(res.get(curHeight), root.val));
            }
            dfs(root.left, res, curHeight + 1);
            dfs(root.right, res, curHeight + 1);
        }
    }

    /**
     * 广度优先遍历
     */
    class Solution2 {
        public List<Integer> largestValues(TreeNode root) {
            //广度优先遍历
            List<Integer> res = new ArrayList<>();
            if (root == null) return res;
            Deque<TreeNode> queue = new ArrayDeque<>();
            queue.addLast(root);
            while (!queue.isEmpty()) {
                int len = queue.size();
                int max = Integer.MIN_VALUE;
                while (len > 0) {
                    len--;
                    TreeNode cur = queue.pollFirst();
                    if (cur.left != null) queue.addLast(cur.left);
                    if (cur.right != null) queue.addLast(cur.right);
                    max = Math.max(max, cur.val);
                }
                res.add(max);
            }
            return res;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution1 {
        public List<Integer> largestValues(TreeNode root) {
            List<Integer> res = new ArrayList<>();
            if (root == null) return res;
            Deque<TreeNode> queue = new ArrayDeque<>();
            queue.addLast(root);
            while (!queue.isEmpty()) {
                int len = queue.size();
                int max = Integer.MIN_VALUE;
                while (len > 0) {
                    len--;
                    TreeNode cur = queue.pollFirst();
                    max = Math.max(max, cur.val);
                    if (cur.left != null) queue.addLast(cur.left);
                    if (cur.right != null) queue.addLast(cur.right);
                }
                res.add(max);
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}